package com.hcb.micro.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.IncentiveSystem;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.service.IIncentiveSystemService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("incentiveSystem")
public class IncentiveSystemController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IIncentiveSystemService incentiveSystemService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();

		List<IncentiveSystem> isAllList = incentiveSystemService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<IncentiveSystem> isList = incentiveSystemService.selectByPara(para);
		
		if(isList.size() <= 0){
			json.put("result", "1");
			json.put("description", "奖励机制列表不存在");
			return buildReqJsonObject(json);
		}
		
		List<Map<String,Object>> list = new ArrayList<>();
		for(IncentiveSystem is:isList){
			Map<String,Object> map = new HashMap<>();
			
			map.put("id", is.getId()+"");
			map.put("conditionCount", is.getConditionCount());
			map.put("conditionPrice", is.getConditionPrice());
			map.put("incentiveContent", is.getIncentiveContent());
			map.put("orderNumber", is.getOrderNumber());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取奖励机制成功");
		json.put("list", list);
		json.put("count", isAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("incentiveSystemId") == null || "".equals(bodyInfo.get("incentiveSystemId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		IncentiveSystem incentiveSystem = incentiveSystemService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("incentiveSystemId")));
		
		if(incentiveSystem == null){
			json.put("result", "1");
			json.put("description", "奖励机制信息不存在");
			return buildReqJsonObject(json);
		}
		Map<String,Object> map = new HashMap<>();
		
		map.put("id", incentiveSystem.getId()+"");
		map.put("conditionCount", incentiveSystem.getConditionCount());
		map.put("conditionPrice", incentiveSystem.getConditionPrice());
		map.put("incentiveContent", incentiveSystem.getIncentiveContent());
		map.put("orderNumber", incentiveSystem.getOrderNumber());
		
    	json.put("result", "0");
		json.put("description", "获取奖励机制信息成功");
		json.put("incentiveSystem", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("conditionCount") == null || "".equals(bodyInfo.get("conditionCount"))){
			right = 0;
		}else if(bodyInfo.get("conditionPrice") == null || "".equals(bodyInfo.get("conditionPrice"))){
			right = 0;
		}else if(bodyInfo.get("incentiveContent") == null || "".equals(bodyInfo.get("incentiveContent"))){
			right = 0;
		}else if(bodyInfo.get("orderNumber") == null || "".equals(bodyInfo.get("orderNumber"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		IncentiveSystem incentiveSystem = new IncentiveSystem();
		
		incentiveSystem.setConditionCount(bodyInfo.getInt("conditionCount"));
		incentiveSystem.setConditionPrice(Float.valueOf(bodyInfo.getString("conditionPrice")));
		incentiveSystem.setIncentiveContent(bodyInfo.getString("incentiveContent"));
		incentiveSystem.setOrderNumber(bodyInfo.getInt("orderNumber"));
		
		Integer insert = incentiveSystemService.insertSelective(incentiveSystem);
		if(insert != 1){
			System.out.println("---新增奖励机制失败---");
			json.put("result", "0");
			json.put("description", "新增奖励机制失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "新增奖励机制成功");
		json.put("incentiveSystemId", incentiveSystem.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("incentiveSystemId") == null || "".equals(bodyInfo.get("incentiveSystemId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		IncentiveSystem incentiveSystem = incentiveSystemService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("incentiveSystemId")));
		if(incentiveSystem == null){
			json.put("result", "1");
			json.put("description", "奖励机制信息不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("conditionCount") != null && !"".equals(bodyInfo.get("conditionCount"))){
			incentiveSystem.setConditionCount(bodyInfo.getInt("conditionCount"));
		}
		if(bodyInfo.get("conditionPrice") != null && !"".equals(bodyInfo.get("conditionPrice"))){
			incentiveSystem.setConditionPrice(Float.valueOf(bodyInfo.getString("conditionPrice")));
		}
		if(bodyInfo.get("incentiveContent") != null && !"".equals(bodyInfo.get("incentiveContent"))){
			incentiveSystem.setIncentiveContent(bodyInfo.getString("incentiveContent"));
		}
		if(bodyInfo.get("orderNumber") != null && !"".equals(bodyInfo.get("orderNumber"))){
			incentiveSystem.setOrderNumber(bodyInfo.getInt("orderNumber"));
		}
		
		Integer insert = incentiveSystemService.updateByPrimaryKeySelective(incentiveSystem);
		if(insert != 1){
			System.out.println("---更新奖励机制失败---");
			json.put("result", "0");
			json.put("description", "更新奖励机制失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "更新奖励机制成功");
		json.put("incentiveSystemId", incentiveSystem.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("incentiveSystemId") == null || "".equals(bodyInfo.get("incentiveSystemId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		IncentiveSystem incentiveSystem = incentiveSystemService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("incentiveSystemId")));
		if(incentiveSystem == null){
			json.put("result", "1");
			json.put("description", "奖励机制信息不存在");
			return buildReqJsonObject(json);
		}
		
		Integer delete = incentiveSystemService.deleteByPrimaryKey(incentiveSystem.getId());
		if(delete != 1){
			System.out.println("---删除奖励机制失败---");
			json.put("result", "0");
			json.put("description", "删除奖励机制失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除奖励机制成功");
		return buildReqJsonObject(json);
	}
}
